package com.dd.cloud.common.utils;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class DateUtils {
    /**
     * 年月日
     */
    public static final SimpleDateFormat SDF_YMD;
    /**
     * 年月日 时分秒
     */
    public static final SimpleDateFormat SDF_YMD_HMS;
    /**
     * 年月日
     */
    public static final DateTimeFormatter DTF_YMD;
    /**
     * 年月日 时分秒
     */
    public static final DateTimeFormatter DTF_YMD_HMS;
    /**
     * 年月日 时分
     */
    public static final DateTimeFormatter DTF_YMD_HM;
    /**
     * 年月日 中文
     */
    public static final DateTimeFormatter DTF_YMD_ZH;
    /**
     * 年月日 时分秒
     */
    public static final DateTimeFormatter DTF_YMD_HMS_ZH;
    /**
     * 年月日 中文
     */
    public static final DateTimeFormatter DTFYMD;
    /**
     * 年月日 时分秒
     */
    public static final DateTimeFormatter DTFYMDHMS;
    /**
     * 年月日 时分秒 毫秒
     */
    public static final DateTimeFormatter DTFYMDHMSS;
    /**
     * 年月日 时分秒
     */
    public static final DateTimeFormatter DTF_MD_HM;

    static {
        SDF_YMD = new SimpleDateFormat("yyyy-MM-dd");
        SDF_YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DTF_YMD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DTF_YMD_ZH = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        DTF_YMD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DTF_YMD_HM = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        DTF_YMD_HMS_ZH = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
        DTFYMD = DateTimeFormatter.ofPattern("yyyyMMdd");
        DTFYMDHMS = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        DTFYMDHMSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        DTF_MD_HM = DateTimeFormatter.ofPattern("MM-dd HH:mm");
    }

    /**
     * 获取今天时间
     *
     * @return long 时间戳
     */
    public static long getDayInMillis() {
        Calendar calDay = Calendar.getInstance();
        calDay.set(Calendar.HOUR_OF_DAY, 0);
        calDay.set(Calendar.MINUTE, 0);
        calDay.set(Calendar.SECOND, 0);
        calDay.set(Calendar.MILLISECOND, 0);
        return calDay.getTimeInMillis();
    }

    /**
     * 获取今天时间
     *
     * @return Date
     */
    public static Date getDayTime() {
        Calendar calDay = Calendar.getInstance();
        calDay.set(Calendar.HOUR_OF_DAY, 0);
        calDay.set(Calendar.MINUTE, 0);
        calDay.set(Calendar.SECOND, 0);
        calDay.set(Calendar.MILLISECOND, 0);
        return calDay.getTime();
    }

    /**
     * 返回今日时间
     *
     * @return String yyyy-MM-dd
     */
    public static String getDate_ymd() {
        return SDF_YMD.format(getDayTime());
    }

    /**
     * 返回今日时间
     *
     * @return String yyyy-MM-dd HH:mm:ss
     */
    public static String getDate_ymdhms() {
        return SDF_YMD_HMS.format(getDayTime());
    }

    /**
     * 获取本周第一天时间
     *
     * @return long 时间戳
     */
    public static long getWeekInMillis() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.WEEK_OF_MONTH, 0);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取本周第一天时间
     *
     * @return Date
     */
    public static Date getWeekTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, 2);//1 星期日 2 星期一
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取本月第一天时间
     *
     * @return long 时间戳
     */
    public static long getMonthInMillis() {
        Calendar calMonth = Calendar.getInstance();
        calMonth.set(Calendar.DAY_OF_MONTH, 1);
        calMonth.set(Calendar.HOUR_OF_DAY, 0);
        calMonth.set(Calendar.MINUTE, 0);
        calMonth.set(Calendar.SECOND, 0);
        return calMonth.getTimeInMillis();
    }

    /**
     * 获取本月第一天时间
     *
     * @return Date
     */
    public static Date getMontTime() {
        Calendar calMonth = Calendar.getInstance();
        calMonth.set(Calendar.DAY_OF_MONTH, 1);
        calMonth.set(Calendar.HOUR_OF_DAY, 0);
        calMonth.set(Calendar.MINUTE, 0);
        calMonth.set(Calendar.SECOND, 0);
        return calMonth.getTime();
    }

    /**
     * 获取本年第一月的第一天时间
     *
     * @return long 时间戳
     */
    public static long getYearInMillis() {
        Calendar calYear = Calendar.getInstance();
        calYear.set(Calendar.DAY_OF_YEAR, 1);
        calYear.set(Calendar.HOUR_OF_DAY, 0);
        calYear.set(Calendar.MINUTE, 0);
        calYear.set(Calendar.SECOND, 0);
        return calYear.getTimeInMillis();
    }

    /**
     * 获取本年第一月的第一天时间
     *
     * @return Date
     */
    public static Date getYearTime() {
        Calendar calYear = Calendar.getInstance();
        calYear.set(Calendar.DAY_OF_YEAR, 1);
        calYear.set(Calendar.HOUR_OF_DAY, 0);
        calYear.set(Calendar.MINUTE, 0);
        calYear.set(Calendar.SECOND, 0);
        return calYear.getTime();
    }


    /**
     * 将Date类型转换成String类型 格式yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getTime_ymd(Date date) {
        return SDF_YMD.format(date);
    }

    /**
     * 将Date类型转换成String类型 格式yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getTime_ymdhms(Date date) {
        return SDF_YMD_HMS.format(date);
    }


    /**
     * 将String类型转换成Date类型
     *
     * @param date yyyy-MM-dd
     * @return
     */
    public static Date getDate_ymd(String date) {
        if (date.length() != 10) {
            return null;
        }
        try {
            return SDF_YMD.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将String类型转换成Date类型
     *
     * @param date yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getDate_ymdhms(String date) {
        if (date.length() != 19) {
            return null;
        }
        try {
            return SDF_YMD_HMS.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Date转换成LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate ofLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

    /**
     * String转换成LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate ofLocalDate(String date) {
        if (date == null) {
            return null;
        }
        if (date.length() < 10) {
            return null;
        }
        date = date.substring(0, 10);
        Date date1 = getDate_ymd(date);
        Instant instant = date1.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

    /**
     * Date转换成LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime ofLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    /**
     * String转换成LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime ofLocalDateTime(String date) {
        if (date == null) {
            return null;
        }
        Date date1 = getDate_ymdhms(date);
        Instant instant = date1.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }


    /**
     * 根据传入的时间 获取本月开始时间
     *
     * @param month
     * @return
     */
    public static LocalDateTime staTime(int year, int month) {
        Date sTime = getBeginDayOfMonth(year, month);
        Instant instant = sTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 根据传入的时间 获取本月结束时间
     *
     * @param month
     * @return
     */
    public static LocalDateTime endTime(int year, int month) {
        Date sTime = getEndDayOfMonth(year, month);
        Instant instant = sTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    //获取本月的开始时间
    public static Date getBeginDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    //获取本月的结束时间
    public static Date getEndDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(year, month - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 转换时间戳
     *
     * @param time
     * @return
     */
    public static long toTimestamp(LocalDateTime time) {
        //东八区时间
        return time.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    /**
     * 获取时间的天开始时间
     *
     * @param now 时间
     * @return
     */
    public static LocalDateTime getStartTimeOfDay(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now, LocalTime.MIN);
    }

    /**
     * 获取时间的天结束时间
     *
     * @return
     */
    public static LocalDateTime todayEndTime(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now, LocalTime.MAX);
    }

    /**
     * 获取时间的周开始时间
     *
     * @param now 时间
     * @return
     */
    public static LocalDateTime getFirstDayOfWeek(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now, LocalTime.MIN).with(DayOfWeek.MONDAY);
    }

    /**
     * 获取时间的周结束时间
     *
     * @param now 时间
     * @return
     */
    public static LocalDateTime getLastDayOfWeek(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now, LocalTime.MAX).with(DayOfWeek.SUNDAY);
    }

    /**
     * 获取时间的月开始时间
     *
     * @param now 时间
     * @return
     */
    public static LocalDateTime getFirstDayOfMonth(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
    }

    /**
     * 获取时间的月结束时间
     *
     * @param now 时间
     * @return
     */
    public static LocalDateTime getLastDayOfMonth(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
    }

    /**
     * 获取季度开始时间
     *
     * @return
     */
    public static LocalDateTime quarterStartTime(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
        return LocalDateTime.of(LocalDate.of(now.getYear(), month, 1), LocalTime.MIN);
    }

    /**
     * 获取季度结束时间
     *
     * @return
     */
    public static LocalDateTime quarterEndTime(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue()).plus(2L);
        return LocalDateTime.of(LocalDate.of(now.getYear(), month, month.length(now.isLeapYear())), LocalTime.MAX);
    }

    /**
     * 获取年开始时间
     *
     * @return
     */
    public static LocalDateTime yearStartTime(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
    }

    /**
     * 获取年结束时间
     *
     * @return
     */
    public static LocalDateTime yearEndTime(LocalDate now) {
        if (now == null) {
            now = LocalDate.now();
        }
        return LocalDateTime.of(now.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
    }

    /**
     * 将日期转换为 上旬下旬
     *
     * @param now
     * @return 2021年1月上旬
     */
    public static String formatEarlyLate(LocalDateTime now) {
        return now.getYear() + "年" + now.getMonth().getValue() + "月" + (now.getDayOfMonth() > 15 ? "下旬" : "上旬");
    }

    public static LocalDateTime[] earlyLateToTimeRange(String format) {
        int index = format.indexOf("上旬");
        String month = index > 6 ? format.replace("上旬", "") : format.replace("下旬", "");
        month = month.replace("年", "-").replace("月", "");
        String[] arr = month.split("-");
        LocalDateTime start;
        LocalDateTime end;
        if (index > 6 && arr.length == 2) {
            start = LocalDateTime.of(Integer.valueOf(arr[0]), Integer.valueOf(arr[1]), 1, 0, 0);
            end = LocalDateTime.of(Integer.valueOf(arr[0]), Integer.valueOf(arr[1]), 15, 23, 59, 59);
        } else {
            start = LocalDateTime.of(Integer.valueOf(arr[0]), Integer.valueOf(arr[1]), 16, 0, 0);
            end = start.with(TemporalAdjusters.lastDayOfMonth()).plusHours(23).plusMinutes(59).plusSeconds(59);
        }
        LocalDateTime[] range = new LocalDateTime[]{start, end};
        return range;
    }

    /**
     * 获取当前日期所在季度的开始日期和结束日期
     * 季度一年四季， 第一季度：1月-3月， 第二季度：4月-6月， 第三季度：7月-9月， 第四季度：10月-12月
     *
     * @param isFirst true表示查询本季度开始日期  false表示查询本季度结束日期
     * @return
     */
    public static LocalDate getStartOrEndDayOfQuarter(Boolean isFirst) {
        LocalDate today = LocalDate.now();
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }

    /**
     * 获取当前日期之后的日期
     *
     * @param type 1日 2周 3月 4季度 5年
     * @return
     */
    public static LocalDate datePlusDays(Integer type) {
        LocalDate date = LocalDate.now();
        switch (type) {
            case 1:
                return date.plusDays(1);
            case 2:
                return date.plusDays(7);
            case 3:
                return date.plusDays(30);
            case 4:
                return date.plusDays(90);
            case 5:
                return date.plusDays(356);
        }
        return date;
    }

    /**
     * 获取时间段内所有季度
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    public static List<String> getSeasonList(LocalDate startTime, LocalDate endTime) {
        // 取当月第一天, 避免startTime的日期大于endTime计算不出来的情况
        startTime = LocalDate.of(startTime.getYear(), startTime.getMonthValue(), 1);
        endTime = LocalDate.of(endTime.getYear(), endTime.getMonthValue(), 1);
        Set<String> set = new HashSet<>();
        LocalDate mark = startTime;
        while (true) {
            if (mark.isBefore(endTime) || mark.isEqual(endTime)) {
                String season = String.valueOf(mark.getYear()) + (mark.getMonthValue() + 2) / 3;
                set.add(season);
                // 加一个月
                mark = mark.plusMonths(1);
            } else {
                break;
            }
        }
        System.out.println(set);
        // set中是倒序, 重新排序
        return set.stream().sorted().collect(Collectors.toList());
    }

    /**
     * 判断多个时间段是否有重叠（交集）
     *
     * @param rangeList 时间段数组
     * @param isStrict  是否严格重叠，true 严格，没有任何相交或相等；false 不严格，可以首尾相等
     * @return 返回是否重叠
     */
    public static boolean isOverlap(List<Range> rangeList, boolean isStrict) {
        rangeList = rangeList.stream().sorted(Comparator.comparingLong(Range::getStart)).collect(Collectors.toList());
        for (int i = 1; i < rangeList.size(); i++) {
            if (isStrict) {
                if (!(rangeList.get(i - 1).getEnd() < rangeList.get(i).getStart())) {
                    return true;
                }
            } else {
                if (!(rangeList.get(i - 1).getEnd() <= rangeList.get(i).getStart())) {
                    return true;
                }
            }
        }
        //最后一个班次如果跨天
        return rangeList.get(rangeList.size() - 1).getEnd() < rangeList.get(rangeList.size() - 1).getStart() && rangeList.get(rangeList.size() - 1).getEnd() > rangeList.get(0).getStart();
    }

    /**
     * date转 2020-08-15 17:20:34 格式
     *
     * @param date
     * @return string
     */
    public static String dateToString(LocalDate date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();
        String localDateStr = localDate.format(fmt);
        return localDateStr;
    }

    public static String dateTimeToString(LocalDateTime date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateStr = localDateTime.format(fmt);
        return dateStr;
    }

    public static String dateMetinueToString(LocalDateTime date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm");
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateStr = localDateTime.format(fmt);
        return dateStr;
    }


    public static long nowUtilEndDate() {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 将时设为0
        calendar.set(Calendar.MINUTE, 0); // 将分设为0
        calendar.set(Calendar.SECOND, 0); // 将秒设为0
        calendar.add(Calendar.DAY_OF_MONTH, 1); // 日期加1天
        calendar.add(Calendar.MILLISECOND, -1); // 减去1毫秒
        Date endTime = calendar.getTime();
        // 步骤3：计算时间间隔
        long interval = endTime.getTime() - currentTime.getTime();

        // 输出时间间隔
        log.info("当前时间到当天24点的间隔为：" + interval + "毫秒");
        return interval;
    }

    @Data
    public static class Range {
        private Integer sort;
        private long start;
        private long end;
    }

    public static Range handle(LocalTime startTime, LocalTime endTime) {
        Range range = new Range();
        LocalDate nowDate = LocalDate.now();
        Instant instantS = LocalDateTime.of(nowDate, startTime).atZone(ZoneId.systemDefault()).toInstant();
        Date s = Date.from(instantS);
        Instant instantE = LocalDateTime.of(nowDate, endTime).atZone(ZoneId.systemDefault()).toInstant();
        Date e = Date.from(instantE);
        range.setStart(s.getTime());
        range.setEnd(e.getTime());
        return range;
    }


    /**
     * 时间搓转为LocalDataTime
     */
    public static LocalDateTime timestamToDatetime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * LocalDataTime转为时间搓
     */
    public static Long datatimeToTimestamp(LocalDateTime ldt) {
        long timestamp = ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return timestamp;
    }

    /**
     * @param startTime: 开始时间
     * @param endTime:   结束时间
     * @return List<String> 如:[2022-07，2022-08，2022-09，2022-10]
     * @description 获取两个日期之间的所有月份 (年月)
     * @author czg
     * @date 2023/4/19 20:54
     */
    public static List<String> getMonthBetweenDate(LocalDate startTime, LocalDate endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = Date.from(startTime.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endTime.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {

                // 把日期添加到集合
                list.add(sdf.format(startDate));

                // 设置日期
                calendar.setTime(startDate);

                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);

                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过年龄数和年龄类型 算出日期
     *
     * @param ageNum
     * @param ageType
     * @return
     */
    public static LocalDate byAgeNumAndAgeTypeToDate(Integer ageNum, Integer ageType) {
        if (ageNum == null || ageType == null) {
            return null;
        }
        LocalDate now = LocalDate.now();
        if (ageType == 1) {
            now = now.minusMonths(ageNum);
        } else if (ageType == 2) {
            now = now.minusYears(ageNum);
        }
        return now;
    }

    public static String extractYearMonthDayOfIdCard(String idCard) {
        String year = null;
        String month = null;
        String day = null;
        //正则匹配身份证号是否是正确的，15位或者17位数字+数字/x/X
        if (idCard.matches("^\\d{15}|\\d{17}[\\dxX]$")) {
            year = idCard.substring(6, 10);
            month = idCard.substring(10, 12);
            day = idCard.substring(12, 14);
        } else {
            System.out.println("身份证号码不匹配！");
            return null;
        }
        return year + "-" + month + "-" + day;
    }

    /**
     * @param format 日期格式
     * @Description 过去6个月 (半年)
     * @Throws
     * @Return java.lang.String
     * @Date 2021-02-02 10:47:22
     * @Author WangKun
     **/
    public static String pastHalfYear(String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -6);
        return sd.format(c.getTime());
    }
}
